﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace SlamCms.SharePoint.Migration
{
	public class MigratableDocumentListItem : MigratableListItem, IMigratable
	{
		public int ListRelativeId { get; set; }
		public string Url { get; set; }
		public string FileId { get; set; }

		public MigratableDocumentListItem()
			: base()
		{
		}

		public override XElement ToXml()
		{
			XElement listItemElement = new XElement("DocumentListItem");
			listItemElement.Add(new XAttribute("ListRelativeId", ListRelativeId));
			listItemElement.Add(new XAttribute("FileId", String.IsNullOrEmpty(FileId) ? Guid.NewGuid().ToString() : FileId));
			if (!String.IsNullOrEmpty(SlamId))
				listItemElement.Add(new XAttribute("SlamId", SlamId));
			listItemElement.Add(new XAttribute("Url", Url));
			XElement fieldValuesElement = new XElement("FieldValues");
			foreach (var fieldValue in FieldValues)
			{
				XElement fieldValueElement = new XElement("FieldValue");
				fieldValueElement.Add(new XAttribute("Name", fieldValue.Key));
				if (fieldValue.Value != null)
					fieldValueElement.Add(new XAttribute("Value", fieldValue.Value));
				fieldValuesElement.Add(fieldValueElement);
			}
			listItemElement.Add(fieldValuesElement);
			return listItemElement;
		}

		public override void FromXml(XElement element)
		{
			ListRelativeId = int.Parse(element.Attribute("ListRelativeId").Value);
			FileId = element.Attribute("FileId").Value;
			Url = element.Attribute("Url").Value;
			base.FromXml(element);
		}
	}
}
